package com.liu.my_blog_system.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liu.my_blog_system.mapper.TbCategoryMapper;
import com.liu.my_blog_system.entity.TbCategory;
import com.liu.my_blog_system.service.TbCategoryService;
import com.liu.my_blog_system.utils.Result;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * tb_category (TbCategory)表服务实现类
 *
 * @author liuchengjie
 * @since 2021-04-07 14:17:55
 */
@Service
@Transactional
public class TbCategoryServiceImpl extends ServiceImpl<TbCategoryMapper, TbCategory> implements TbCategoryService {
    /**
     * 新增分类
     * @param category
     * @return
     */
    @Override
    public Result addCategory(TbCategory category) {

        int row = baseMapper.insert(category);
        if (row > 0) {
            return Result.ok().message("插入分类成功");
        }
        return Result.error().message("插入分类失败");
    }

    /**
     * 分页查询所有分类
     * @param current
     * @param size
     * @return
     */
    @Override
    public Result findCategoryPage(Long current, Long size) {
        Page<TbCategory> categoryPage = new Page<>(current,size);
        Page<TbCategory> page = baseMapper.selectPage(categoryPage, null);
        long total = page.getTotal();
        List<TbCategory> records = page.getRecords();
        return Result.ok().data("total",total).data("records",records);
    }
}
